package com.mkb.service.impl;

import java.util.List;

import com.mkb.entity.AreaDO;
import com.mkb.entity.AreaDTO;
import com.mkb.utils.MapUtils;
import com.zz.common.util.ObjectUtils;
import com.zz.common.validator.Assert;
import com.zz.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mkb.dao.AreaDAO;
import com.mkb.service.AreaService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class AreaServiceImpl implements AreaService {
	@Autowired
	private AreaDAO areaDAO;

	@Override
	public Boolean deleteById(Long id) {
		areaDAO.deleteById(id);
		return true;
	}

	@Override
	public Boolean create(AreaDTO record) throws Exception {
		AreaDO entity = record.clone(AreaDO.class);
		areaDAO.insert(entity);
		return true;
	}

	@Override
	public AreaDTO getById(Long id) throws Exception{
		Assert.isNull(id, "id 不能为空");
		AreaDO entity= areaDAO.getById(id);
		Assert.isNull(entity, "对象不存在");
		AreaDTO dto = entity.clone(AreaDTO.class);
		return dto;
	}

	@Override
	public Boolean update(AreaDTO record) throws Exception {
		Assert.isNull(record, "对象不存在");
		Assert.isNull(record.getId(), "对象id不存在");
		AreaDO entity = record.clone(AreaDO.class);
		areaDAO.update(entity);
		return true;
	}

	@Override
	public PageInfo<AreaDTO> queryPage(AreaDTO areaDTO) throws Exception {
	        ValidatorUtils.validateProperty(areaDTO, "pageNum", "pageSize");
		PageHelper.startPage(areaDTO.getPageNum(), areaDTO.getPageSize());
		List<AreaDO> areaDOS = areaDAO.queryList(MapUtils.parseMap(areaDTO));
		List<AreaDTO> areaDTOList = ObjectUtils.convertList(areaDOS, AreaDTO.class);
		return new PageInfo<>(areaDTOList);
	}

	@Override
	public List<AreaDTO> queryList(AreaDTO areaDTO) throws Exception {
		List<AreaDO> areaDOS = areaDAO.queryList(MapUtils.parseMap(areaDTO));
		List<AreaDTO> areaDTOList = ObjectUtils.convertList(areaDOS, AreaDTO.class);
		return areaDTOList;
	}

}